package com.shijimo.${module}.controller.admin;

import com.shijimo.server.dto.${Domain}Dto;
import com.shijimo.server.dto.PageDto;
import com.shijimo.server.dto.ResponseDto;
import com.shijimo.server.service.${Domain}Service;
import com.shijimo.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Dream_飞翔
 * @email 1072876976@qq.com
 */
@RestController
@RequestMapping("/admin/${domain}")
public class ${Domain}Controller {

    private static final Logger LOG = LoggerFactory.getLogger(${Domain}Controller.class);
    public static final String BUSINESS_NAME = "${tableNameCn}";

    @Resource
    private ${Domain}Service ${domain}Service;

    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        LOG.info("pageDto：{}", pageDto);
        // 进行查询
        ${domain}Service.list(pageDto);
        // 设置返回前端的对象，默认成功
        responseDto.setContent(pageDto);
        return responseDto;
    }

    @PostMapping("/save")
    public ResponseDto save(@RequestBody ${Domain}Dto ${domain}Dto) {
        LOG.info("${domain}Dto：{}", ${domain}Dto);

        // 保存校验
        <#list fieldList as field>
        <#if field.name != "id" && field.nameHump != "createdAt" && field.nameHump != "updatedAt" && field.nameHump != "sort">
            <#if !field.nullAble>
        ValidatorUtil.require(${domain}Dto.get${field.nameBigHump}(), "${field.nameCn}");
            </#if>
            <#--  如果字段长度大于0就去校验字段的长度  -->
            <#if (field.length > 0)>
                <#--  数值后面加一个"?c"的意义是将数值转换成字符串的一个函数，对数字做一个字符串的处理  -->
        ValidatorUtil.length(${domain}Dto.get${field.nameBigHump}(), "${field.nameCn}", 1, ${field.length?c});
            </#if>
        </#if>
        </#list>
        ResponseDto responseDto = new ResponseDto();
        // 在数据持久层中进行数据的存储
        ${domain}Service.save(${domain}Dto);
        responseDto.setContent(${domain}Dto);
        return responseDto;
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        LOG.info("id：{}", id);
        // 执行删除方法
        ${domain}Service.delete(id);
        return responseDto;
    }
}
